package com.itmck.control;

import com.alibaba.fastjson.JSON;
import com.itmck.service.DynamicRouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 太阳当空照,花儿对我笑
 * <p>
 * Create by M ChangKe 2021/11/3 15:24
 * <p>
 * 路由控制器
 **/
@Slf4j
@RestController
public class GatewayController {

    @Resource
    private DynamicRouteService dynamicRouteService;


    @PostMapping("/gateway/addRoute")
    public String addRoute(RouteDefinition definition) {
        log.info("添加网关路由规则:{}", JSON.toJSONString(definition));
        String result = dynamicRouteService.add(definition);
        log.info("添加网关路由规则,响应结果:{}", result);
        return result;
    }

    @GetMapping("/gateway/deleteRoute")
    public String deleteRoute(String id) {
        log.info("删除网关路由规则:{}", id);
        String result = dynamicRouteService.delete(id);
        log.info("删除网关路由规则,响应结果:{}", result);
        return result;
    }

    @PostMapping("/gateway/updateByIdRoute")
    public String updateByIdRoute(RouteDefinition definition) {
        log.info("更新网关路由规则:{}", JSON.toJSONString(definition));
        String result = dynamicRouteService.updateById(definition);
        log.info("更新网关路由规则,响应结果:{}", result);
        return result;
    }
}